//
//  UIImage+Extension.swift
//  WeiBoItem
//
//  Created by jss on 16/4/27.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit

extension UIImage{

    //截图方法
 class func getCutImage()->UIImage {
    
    //取到主窗口
    let window = UIApplication.sharedApplication().keyWindow
    
    //取到主窗口的frame
    let rect = window?.frame
    //开启图形上下文(最后面的参数 0是要接下来的图按原比例显示(按点)比较清楚 1是按像素(模糊)显示 )
    UIGraphicsBeginImageContextWithOptions((rect?.size)!, false, 0)
    //把图片渲染到图像上下文中
    window?.drawViewHierarchyInRect(rect!, afterScreenUpdates: true)
    //获取图片
    let image = UIGraphicsGetImageFromCurrentImageContext()
    //关闭图形上下文
    UIGraphicsEndImageContext()
    //返回图片
    return image
    
}



//图片的缩放方法
func scaleToWidth(width:CGFloat) ->UIImage {

//w1:1200 h1:400
//w2:600  h2:200
    
    if self.size.width < width {
        
        return self
        
    }
    
    //求得h1
    let height = width / self.size.width * self.size.height
    
    let rect = CGRect(x: 0, y: 0, width: width, height: height)
    
    //开启图形上下文
    UIGraphicsBeginImageContext(rect.size)
    //将图片全部内容画到图形上文指定的rect中
    self.drawInRect(rect)
     //获取图片
    let image = UIGraphicsGetImageFromCurrentImageContext()
    //关闭图形上下文
    UIGraphicsEndImageContext()
    //返回图片
    return image
    

}

}


